package util;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.List;
import java.util.Objects;

/**
 * @fileName: Spider
 * @author: Han
 * @date: 2018/5/24 10:45
 * @dscription:
 */
public class Spider {
    /**
     * 获取页面字符串
     *
     * @param urlStr  目标url
     * @param headers 请求头
     * @return 页面
     * @throws Exception 抛出异常
     */
    public String getPageByPost(String urlStr, Header[] headers, List<NameValuePair> nameValuePairList) throws Exception {
        String pageStr = "";
        //建立HTTPClient对象
        HttpClient httpClient = HttpClients.createDefault();
        //建立POST请求
        HttpPost post = null;
        try {
            post = new HttpPost(urlStr);
            post.setHeaders(headers);
            HttpEntity formData = new UrlEncodedFormEntity(nameValuePairList, Consts.UTF_8);
            post.setEntity(formData);
            //发送POST请求
            HttpResponse response = httpClient.execute(post);
            //处理请求
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                // 获取字节数组
                byte[] content = EntityUtils.toByteArray(entity);
                pageStr = new String(content, "utf-8");
            }
        } catch (Exception e) {
            throw new Exception();
        } finally {
            Objects.requireNonNull(post).abort();
        }
        return pageStr;
    }

    /**
     * 使用GET方法获取数据
     *
     * @param urlStr  目标url
     * @param headers 请求头
     * @return 页面
     * @throws Exception 抛出异常
     */
    public String getPageByGet(String urlStr, Header[] headers) throws Exception {
        String pageStr;
        //建立HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
        //建立GET请求
        HttpGet get = null;
        HttpHost proxy = new HttpHost("125.112.200.154",29800);
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        try {
            get = new HttpGet(urlStr);
            get.setConfig(requestConfig);
            //设置请求头
            get.setHeaders(headers);
            //发送GET请求
            HttpResponse response = httpClient.execute(get);
            //处理响应
            //if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                //获取字节数组
                byte[] content = EntityUtils.toByteArray(entity);
                pageStr = new String(content, "utf-8");
                System.out.println(pageStr);
            //}
        } catch (Exception e) {
            e.printStackTrace();
            Objects.requireNonNull(get).abort();
            throw new Exception();
        } finally {
            Objects.requireNonNull(get).abort();
        }

        return pageStr;
    }
}
